using Digitalmes.Application.Mes.Masters.Lines;
using Digitalmes.Application.Mes.Masters.Lines.Commands;
using Digitalmes.Application.Mes.Masters.Lines.Queries;

namespace Digitalmes.WebApi.Endpoints.Mes.Masters;

/// <summary>
/// 产线终结点。
/// </summary>
public sealed class LineEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "mes/master", "line")
            .RequireAuthorization()
            .MapGet(GetAllLineListAsync, "all")
            .MapGet(GetLineListAsync, "list")
            .MapGet(GetLinePagedListAsync, "list-page")
            .MapPost(CreateLineAsync)
            .MapPut(UpdateLineAsync, "{id}")
            .MapDelete(DeleteLineAsync, "{id}");
    }

    [EndpointDescription("查询生产线，包含工站信息")]
    public async Task<IApiResult> GetAllLineListAsync(ISender sender, [AsParameters] GetAllLineListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("查询生产线")]
    public async Task<IApiResult> GetLineListAsync(ISender sender, [AsParameters] GetLineListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("分页查询生产线")]
    public async Task<IApiResult> GetLinePagedListAsync(ISender sender, [AsParameters] GetLineListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建生产线")]
    public async Task<IApiResult> CreateLineAsync(ISender sender, MdLineDto dto)
    {
        var result = await sender.Send(new CreateLineCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新生产线")]
    public async Task<IApiResult> UpdateLineAsync(ISender sender, long id, MdLineDto dto)
    {
        var result = await sender.Send(new UpdateLineCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除生产线")]
    public async Task<IApiResult> DeleteLineAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteLineCommand(id));
        return result.ToApiResult();
    }
}
